﻿' BasecampVB - http://www.tidbitsandtangents.com
' Copyright (c) 2009
' by Rusty S. Swayne
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports TnT.BasecampVB.DAL

Namespace BLL


    Public Class ToDoList
        Inherits BasecampBase

        ''' <summary>
        ''' Constructor
        ''' </summary>
        Public Sub New( _
            ByVal todo_list_id As Integer, _
            ByVal project_id As Integer, _
            ByVal milestone_id As Integer, _
            ByVal name As String, _
            ByVal description As String, _
            ByVal position As Integer, _
            ByVal completed_count As Integer, _
            ByVal uncompleted_count As Integer, _
            ByVal tracked As Boolean, _
            ByVal is_private As Boolean, _
            ByVal complete As Boolean)

            Me.todo_list_id = todo_list_id
            Me.project_id = project_id
            Me.milestone_id = milestone_id
            Me.name = name
            Me.description = description
            Me.position = position
            Me.completed_count = completed_count
            Me.uncompleted_count = uncompleted_count
            Me.tracked = tracked
            Me.is_private = is_private
            Me.complete = complete

        End Sub

        ''' <summary>
        ''' Returns ToDoList from Basecamp API
        ''' </summary>
        Public Shared Function GetToDoList(ByVal todo_list_id As Integer) As ToDoList
            Return GetToDoListFromToDoListDetails(BasecampProvider.API.GetToDoList(todo_list_id))
        End Function

        ''' <summary>
        ''' Returns a List(Of ToDoList) from Basecamp API
        ''' </summary>
        Public Overloads Shared Function GetToDoLists() As List(Of ToDoList)
            Return GetToDoListListFromToDoListDetailsCollection(BasecampProvider.API.GetToDoLists(0))
        End Function

        ''' <summary>
        ''' Returns a List(Of ToDoList) from Basecamp API
        ''' </summary>
        Public Overloads Shared Function GetToDoLists(ByVal project_id As Integer, ByVal filter As ToDoListType) As List(Of ToDoList)
            Return GetToDoListListFromToDoListDetailsCollection(BasecampProvider.API.GetToDoLists(project_id, filter.ToString))
        End Function

        ''' <summary>
        ''' Returns a List(Of ToDoList) from Basecamp API
        ''' </summary>
        Public Overloads Shared Function GetToDoLists(ByVal responsible_party_id As Integer, ByVal responsible_party_type As ResponsiblePartyType) As List(Of ToDoList)
            Dim responsible_party As String = responsible_party_id.ToString
            If responsible_party_type = ResponsiblePartyType.company Then responsible_party = "c" & responsible_party
            Return GetToDoListListFromToDoListDetailsCollection(BasecampProvider.API.GetToDoLists(responsible_party))
        End Function

        ''' <summary>
        ''' Creates a ToDoList in Basecamp via Basecamp API
        ''' </summary>
        Public Shared Function CreateToDoList(ByVal project_id As Integer, ByVal milestone_id As Integer, ByVal name As String, ByVal description As String, ByVal tracked As Boolean, ByVal is_private As Boolean, Optional ByVal todo_list_template_id As Integer = 0) As Integer
            Return BasecampProvider.API.CreateToDoList(project_id, milestone_id, name, description, tracked, is_private, todo_list_template_id)
        End Function

        ''' <summary>
        ''' Updates a ToDoList in Basecamp via Basecamp API
        ''' </summary>
        Public Shared Function UpdateToDoList(ByVal todo_list_id As Integer, ByVal milestone_id As Integer, ByVal name As String, ByVal description As String, ByVal tracked As Boolean, ByVal is_private As Boolean) As Boolean
            Return BasecampProvider.API.UpdateToDoList(todo_list_id, milestone_id, name, description, tracked, is_private)
        End Function

        ''' <summary>
        ''' Updates a ToDoList in Basecamp via Basecamp API
        ''' </summary>
        Public Function Update()
            Return UpdateToDoList(Me.todo_list_id, Me.milestone_id, Me.name, Me.description, Me.tracked, Me.is_private)
        End Function

        ''' <summary>
        ''' Deletes a ToDoList in Basecamp via Basecamp API
        ''' </summary>
        Public Shared Function DeleteToDoList(ByVal todo_list_id As Integer) As Boolean
            Return BasecampProvider.API.DeleteToDoList(todo_list_id)
        End Function

        ''' <summary>
        ''' Deletes a ToDoList in Basecamp via Basecamp API
        ''' </summary>
        Public Function Delete()
            Dim ret As Boolean = BasecampProvider.API.DeleteToDoList(Me.todo_list_id)
            Me.todo_list_id = 0
            Return ret
        End Function

        ''' <summary>
        ''' Reorders ToDoLists in Basecamp via Basecamp API
        ''' </summary>
        Public Shared Function ReorderToDoLists(ByVal project_id As Integer, ByVal todolists As List(Of ToDoList)) As Boolean
            Dim ret As Boolean = False
            Dim ids As New List(Of Integer)
            If todolists IsNot Nothing Then
                Dim tdl As ToDoList = Nothing
                For Each tdl In todolists
                    ids.Add(tdl.todo_list_id)
                Next
                ret = BasecampProvider.API.ReorderToDoLists(project_id, ids)
            End If
            Return ret
        End Function


#Region "Private Utility Methods"

        ''' <summary>
        ''' Helper Function to create a List(Of ToDoList) from List(Of ToDoListDetails) 
        ''' </summary>
        Private Shared Function GetToDoListListFromToDoListDetailsCollection(ByVal recordset As List(Of ToDoListDetails)) As List(Of ToDoList)
            Dim items As New List(Of ToDoList)
            Dim item As ToDoListDetails = Nothing
            For Each item In recordset
                items.Add(GetToDoListFromToDoListDetails(item))
            Next
            Return items
        End Function

        ''' <summary>
        ''' Helper Function to create a ToDoList from ToDoListDetails
        ''' </summary>
        Private Shared Function GetToDoListFromToDoListDetails(ByVal record As ToDoListDetails) As ToDoList
            Return New ToDoList(record.todo_list_id, record.project_id, record.milestone_id, record.name, record.description, record.position, record.completed_count, record.uncompleted_count, record.tracked, record.is_private, record.complete)
        End Function

#End Region

#Region "Custom Properties"

        ''' <summary>
        ''' Returns the number of ToDoItem in currently instantiated ToDoList
        ''' </summary>
        Public ReadOnly Property item_count() As Integer
            Get
                Return completed_count + uncompleted_count
            End Get
        End Property

        ''' <summary>
        ''' Returns a List(Of ToDoItem) for the currently instantiated ToDoList
        ''' </summary>
        Public ReadOnly Property todo_items() As List(Of ToDoItem)
            Get
                Return ToDoItem.GetToDoItems(todo_list_id)
            End Get
        End Property

#End Region

#Region "Properties"

        Private _todo_list_id As Integer
        Public Property todo_list_id() As Integer
            Get
                Return _todo_list_id
            End Get
            Set(ByVal value As Integer)
                _todo_list_id = value
            End Set
        End Property

        Private _project_id As Integer
        Public Property project_id() As Integer
            Get
                Return _project_id
            End Get
            Set(ByVal value As Integer)
                _project_id = value
            End Set
        End Property

        Private _milestone_id As Integer
        Public Property milestone_id() As Integer
            Get
                Return _milestone_id
            End Get
            Set(ByVal value As Integer)
                _milestone_id = value
            End Set
        End Property


        Private _name As String
        Public Property name() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property

        Private _description As String
        Public Property description() As String
            Get
                Return _description
            End Get
            Set(ByVal value As String)
                _description = value
            End Set
        End Property


        Private _position As Integer
        Public Property position() As Integer
            Get
                Return _position
            End Get
            Set(ByVal value As Integer)
                _position = value
            End Set
        End Property

        Private _completed_count As Integer
        Public Property completed_count() As Integer
            Get
                Return _completed_count
            End Get
            Set(ByVal value As Integer)
                _completed_count = value
            End Set
        End Property


        Private _uncompleted_count As Integer
        Public Property uncompleted_count() As Integer
            Get
                Return _uncompleted_count
            End Get
            Set(ByVal value As Integer)
                _uncompleted_count = value
            End Set
        End Property

        Private _tracked As Boolean
        Public Property tracked() As Boolean
            Get
                Return _tracked
            End Get
            Set(ByVal value As Boolean)
                _tracked = value
            End Set
        End Property


        Private _is_private As Boolean
        Public Property is_private() As Boolean
            Get
                Return _is_private
            End Get
            Set(ByVal value As Boolean)
                _is_private = value
            End Set
        End Property

        Private _complete As Boolean
        Public Property complete() As Boolean
            Get
                Return _complete
            End Get
            Set(ByVal value As Boolean)
                _complete = value
            End Set
        End Property


#End Region

    End Class


End Namespace